package com.iye.puretennis.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import com.iye.puretennis.R;
import com.iye.puretennis.activity.test.TabHostViewPagerActivity;
import com.iye.puretennis.helper.Constant;
import com.iye.puretennis.helper.DBManager;
import com.iye.puretennis.helper.RemoteUrlCollections;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Author: Chuanbao on 2014/11/11.
 */
public class LoginPageActivity extends Activity{
    DBManager dbManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginpage);
        dbManager = new DBManager(this);
    }

    public void login(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                EditText usernameView = (EditText) findViewById(R.id.username);
                EditText passwordView = (EditText) findViewById(R.id.password);
                String username = usernameView.getText().toString();
                String password = passwordView.getText().toString();
                process(username, password);
            }
        }).start();

    }

    private void process(String username, String password) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postMethod = new HttpPost(RemoteUrlCollections.LOGIN);
        String auth = username + ":" + password;
        postMethod.addHeader("Authorization", "Basic "+ Base64.encodeToString(auth.getBytes(), Base64.NO_WRAP));
        postMethod.addHeader("Content-Type", "application/json");
        postMethod.addHeader("User-Agent", "imgfornote");
        HttpResponse response = null; //执行POST方法
        String rev = null;
        try {
            response = httpClient.execute(postMethod);
            int statusCode = response.getStatusLine().getStatusCode();
            if(statusCode == Constant.RESPONSE_STATUS_OK){
                rev = EntityUtils.toString(response.getEntity());
                JSONObject result = new JSONObject(rev);
                String guid = result.getString("guid");
                String token = result.getString("token");
                //TODO-- store them in local storage
                dbManager.login(guid,token);
                //start the profile activity
                Intent intent = new Intent(LoginPageActivity.this,MainTabsActivity.class);
                intent.putExtra("guid",guid);
                intent.putExtra("Access_Token",token);
                startActivity(intent);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
